home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d20
/
dorskel2.arc
/
XBBSMSG.ARC
/
MAKEPKT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-12-05
|
4KB
|
164 lines
#ifndef __TURBOC__
#define INCL_DOS
#include <os2.h> /* for DosSleep() */
#include <sys/locking.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <fcntl.h>
#include <share.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "xmsg.h"
#include "pkt.h"
#ifdef __TURBOC__
#define LK_LOCK 1
#define LK_UNLCK 2
#define DosSleep(x) sleep(1)
int pascal locking (int handle,int type,long length);
#define _fastcall pascal
#endif
/* Simple packet building functions than can be used for rescanning,
building mailerless point packets, etc. */
/* global variables created here */
int pkthandle = -1; /* current packet handle */
char currout[257] = ""; /* name of last packet opened */
void _fastcall close_pkt (void) {
/* close the global packet handle */
if(pkthandle != -1) {
close(pkthandle);
pkthandle = -1;
*currout = 0;
}
}
int _fastcall open_pkt (char *filename,AN_ADDR *hisaddr,AN_ADDR *myaddr) {
long pos;
PKTHDR ph;
/* Open (create if necessary) packet requested.
Return global file handle for the packet positioned to eop.
Note that use of a global file handle allows us to speed up
the packet-writing process dramatically and simplify coding
in general, but that it makes this code non-reentrant.
Returns -1 on error, pkthandle on success. */
if(!filename || !filename || !hisaddr || !myaddr)
return -1; /* error in parameters */
if(pkthandle != -1 && !stricmp(filename,currout)
return pkthandle; /* this file already open */
if(pkthandle != -1) { /* close previous packet */
close_pkt();
}
pkthandle = sopen(filename,O_RDWR | O_BINARY | O_CREAT,SH_DENYWR,S_IREAD | S_IWRITE);
if(pkthandle != -1) {
lseek(pkthandle,0L,SEEK_END);
if(!tell(pkthandle)) {
memset(&ph,0,sizeof(PKTHDR));
ph.version = 2;
ph.subver = 2;
ph.dnode = addr->node;
ph.dnet = addr->net;
ph.dzone = addr->zone;
ph.dpoint = addr->point;
strncpy(ph.ddomain,addr->domain,8);
maddr = best_guess(addr,myaddr);
ph.onode = maddr->node;
ph.onet = maddr->net;
ph.ozone = maddr->zone;
ph.opoint = maddr->point;
strncpy(ph.odomain,maddr->domain,8);
write(pkthandle,&ph,sizeof(PKTHDR));
write(pkthandle,"\0",2);
}
pos = tell(pkthandle);
if(pos) lseek(pkthandle,pos - 2L,SEEK_SET); /* Position to next msg spot */
else { /* error */
close_pkt();
unlink(filename);
return -1;
}
}
strcpy(currout,filename);
return pkthandle;
}
int _fastcall write_pkt_msg (int fp,XMSG *amsg,char *text,char *area) {
char pmsg[192],*p;
unsigned int x;
/* Write the message given to the end of packet handle fp as a
packed msg. Always returns 1; not much error-checking. */
memset(pmsg,192,0);
*pmsg = 0x02;
pmsg[1] = 0x00;
memcpy(&pmsg[2],&amsg->orig,2);
memcpy(&pmsg[4],&amsg->dest,2);
memcpy(&pmsg[6],&amsg->orig_net,2);
memcpy(&pmsg[8],&amsg->dest_net,2);
memcpy(&pmsg[10],&amsg->attr,2);
x = 0;
memcpy(&pmsg[12],&x,2);
for(x = 0;x < 20;x++) if(!amsg->date[x]) amsg->date[x] = 'Q';
amsg->date[19] = 0;
memcpy(&pmsg[14],amsg->date,20);
p = &pmsg[34];
strcpy(p,amsg->to);
x = 34;
while(*p){
x++;
p++;
}
p++;
x++;
strcpy(p,amsg->from);
while(*p){
x++;
p++;
}
x++;
p++;
strcpy(p,amsg->subj);
while(*p){
x++;
p++;
}
x++;
p++;
write(fp,pmsg,x);
if(area && *area) { /* Prepend area tag */
write(fp,"AREA: ",6);
write(fp,area,strlen(area));
write(fp,"\r",1);
}
write(fp,text,strlen(text));
write(fp,"\0\0",3);
lseek(fp,(tell(fp) - 2L),SEEK_SET); /* Ready for another msg */
return 1;
}